package com.zryc.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zryc.mapper.OrderMapper;
import com.zryc.shop.entity.Order;

import com.zryc.shop.entity.Product;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

@Service
public class OrderService implements IService<Order> {
    @Autowired
    private OrderMapper orderMapper;
    //注入一个商品微服务的feign客户端
    @Resource
    private ProductServiceClient productServiceClient;

    /**
     * 新建一个生成订单的方法
     * @param pid   商品ID
     * @param num   购买数量
     * @return  生成的订单信息
     */
    @GlobalTransactional
    public Order createOrder(Integer pid,Integer num){
        Product product=productServiceClient.getProd(pid);
        Order order=new Order();
        order.setPname(product.getPname());
        order.setNumber(num);
        order.setPprice(product.getPprice());
        order.setUid(1);
        order.setUsername("测试用户Laoliu");
        order.setPid(pid);
        save(order);//订单数据写入订单表
        //调用商品微服务扣掉响应库存
        productServiceClient.reduceInventory(pid,num);
        return order;//返回订单信息
    }
    @Override
    public BaseMapper<Order> getBaseMapper() {
        return orderMapper;
    }

    @Override
    public boolean saveBatch(Collection<Order> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Order> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Order> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Order entity) {
        return false;
    }

    @Override
    public Order getOne(Wrapper<Order> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Order> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Order> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }
    @Override
    public Class<Order> getEntityClass() {
        return null;
    }
}
